from multiprocessing import  Process,Lock
from multiprocessing import  Queue
from threading import  Thread



def save_to_file(index,lock):
    with lock:
        with open("job.log","a",encoding="utf-8") as f:
            f.write(str(index)+"\n")

def save_to_queue(index,my_queue):
    my_queue.put(index)

zero=0
lock=Lock()
def foo():
    global zero
    for i in range(10):
        with lock:
         zero=i
    print("zero",zero)


#多进程 锁
"""
if __name__ == "__main__":
 procress_array=[]
 lock=Lock()
 for i in range(10):
     p=Process(target=save_to_file,args=(i,lock))
     procress_array.append(p)
     p.start()
 for p in procress_array:
     p.join()
 print("done")
"""


#多线程 锁
if __name__ == "__main__":
 process_array=[]
 for i in range(2):
     t=Thread(target=foo)
     process_array.append(t)
     t.start()
 for p in process_array:
     p.join()

 print(zero)



#多进程  Queue队列
"""
if __name__ == "__main__":
 process_array=[]
 my_queue=Queue()
 for i in range(10):
     p=Process(target=save_to_queue,args=(i,my_queue))
     process_array.append(p)
     p.start()
 for p in process_array:
     p.join()

     print(my_queue.get())

"""
